package org.iks.push.handler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.Attribute;
import lombok.extern.slf4j.Slf4j;
import org.iks.common.constant.RedisConstant;
import org.iks.common.util.IpUtil;
import org.iks.push.constant.ChannelConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author kzou
 * @date 2024/1/3 13:45
 * @description: 权限拦截处理
 */
@ChannelHandler.Sharable
@Component
@Slf4j
public class ChannelAuthInterceptor extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Attribute<String> publicKey = ctx.channel().attr(ChannelConstant.getPublicAddressAttrKey());
        Attribute<String> account = ctx.channel().attr(ChannelConstant.getAccountAttrKey());
        if (account == null || account.get() == null || publicKey == null || publicKey.get() == null) {
            ctx.channel().close();
            log.warn("interceptor a not auth request {}", msg);
            return;
        }
        super.channelRead(ctx, msg);
    }

}
